package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.*;
import com.fire.web.modules.all.mapper.AllUserMapper;
import com.fire.web.modules.all.mapper.FireSupervisorMapper;
import com.fire.web.modules.all.mapper.FireSupervisorUserMapper;
import com.fire.web.modules.all.mapper.UserAuthorityMapper;
import com.fire.web.modules.all.request.AddUserReq;
import com.fire.web.modules.all.service.IFireSupervisorUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 监管单位用户Service
 *
 * @author Matthew
 * @Date 2019-01-10 11:23
 */
@Service
public class FireSupervisorUserServiceImpl extends ServiceImpl<FireSupervisorUserMapper, FireSupervisorUserModel> implements IFireSupervisorUserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireSupervisorUserServiceImpl.class);

	@Autowired
	private FireSupervisorUserMapper fireSupervisorUserMapper;
	@Autowired
	private FireSupervisorMapper fireSupervisorMapper;
	@Autowired
	private AllUserMapper allUserMapper;
	@Autowired
	private UserAuthorityMapper userAuthorityMapper;

	@Override
	public FireSupervisorUserModel selectOne(FireSupervisorUserModel entity) {
		return fireSupervisorUserMapper.selectOne(entity);
	}

	@Override
	public List<FireSupervisorUserModel> selectList(FireSupervisorUserModel model) {
		return fireSupervisorUserMapper.selectList(model, new EntityWrapper<FireSupervisorUserModel>());
	}

	@Override
	public List<FireSupervisorUserModel> selectList(FireSupervisorUserModel model, Wrapper<FireSupervisorUserModel> wrapper) {
		return fireSupervisorUserMapper.selectList(model, wrapper);
	}

	@Override
	public List<FireSupervisorUserModel> selectPage(Pagination pagination, FireSupervisorUserModel model, Wrapper<FireSupervisorUserModel> wrapper) {
		return fireSupervisorUserMapper.selectPage(pagination, model, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addSupUser(AddUserReq req, String adminid) {

		//先添加t_mit_user获得userid
		AllUserModel allUserModel = new AllUserModel();
		allUserModel.setAliasname(req.getUsername());
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
		allUserModel.setPhone(req.getPhone());
		allUserModel.setCreatetime(new Date());
		allUserModel.setUpdatetime(allUserModel.getUpdatetime());
		allUserModel.setChannelid(1);
		allUserModel.setChannelname("fire");
		allUserMapper.insert(allUserModel);
		AllUserModel select = new AllUserModel();
		select.setPhone(req.getPhone());
		allUserModel = allUserMapper.selectOne(select);

		//根据adminid获取监管单位id
		FireSupervisorModel fireSupervisormodel = new FireSupervisorModel();
		fireSupervisormodel.setAdminid(adminid);
		fireSupervisormodel = fireSupervisorMapper.selectOne(fireSupervisormodel);

		//插入监管单位用户
		FireSupervisorUserModel fireSupervisorUserModel = new FireSupervisorUserModel();
		fireSupervisorUserModel.setUsername(req.getUsername());
		fireSupervisorUserModel.setPhone(req.getPhone());
		fireSupervisorUserModel.setImageurl(req.getImageurl());
		fireSupervisorUserModel.setIdentification(req.getIdentification());
		fireSupervisorUserModel.setCreatetime(new Date());
		fireSupervisorUserModel.setUpdatetime(fireSupervisorUserModel.getCreatetime());
		fireSupervisorUserModel.setAdminid(adminid);
		fireSupervisorUserModel.setSupervisor(fireSupervisormodel.getSupervisorid());
		fireSupervisorUserModel.setUserid(allUserModel.getUserid());
		fireSupervisorUserMapper.insert(fireSupervisorUserModel);
		//插入业主权限
		UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
		userAuthorityModel.setUserid(allUserModel.getUserid());
		userAuthorityModel.setAuthority("supervisor");
		userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
		userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
		userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
		userAuthorityModel.setCreatetime(new Date());
		userAuthorityModel.setUpdatetime(userAuthorityModel.getCreatetime());
		userAuthorityMapper.insert(userAuthorityModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS,"新增用户成功","新增用户成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult updateSupUser(AddUserReq req) {
		//先更新t_mit_user
		AllUserModel allUserModel = new AllUserModel();
		allUserModel.setUserid(req.getUserid());
		allUserModel.setAliasname(req.getUsername());
		//查询电话是否重复
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().ne("userid", req.getUserid()).eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
		allUserModel.setPhone(req.getPhone());
		allUserModel.setUpdatetime(new Date());
		allUserMapper.updateById(allUserModel);

		//再更新监管用户表
		FireSupervisorUserModel fireSupervisorUserModel = new FireSupervisorUserModel();
		fireSupervisorUserModel.setUserid(req.getUserid());
		fireSupervisorUserModel.setPhone(req.getPhone());
		fireSupervisorUserModel.setIdentification(req.getIdentification());
		fireSupervisorUserModel.setUsername(req.getUsername());
		fireSupervisorUserModel.setImageurl(req.getImageurl());
		fireSupervisorUserModel.setUpdatetime(new Date());
		fireSupervisorUserMapper.updateById(fireSupervisorUserModel);

		//插入业主权限
		UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
		userAuthorityModel.setUserid(allUserModel.getUserid());
		userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
		userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
		userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
		userAuthorityModel.setUpdatetime(new Date());
		userAuthorityMapper.updateById(userAuthorityModel);

		return new AjaxResult(ErrorCodeConstants.SUCCESS,"更新用户成功","更新用户成功");
	}

}
